package com.keyancompany.zhongbaopingtai.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.MemberAdapter;
import com.keyancompany.zhongbaopingtai.api.FriendsAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.EventBusEntity;
import com.keyancompany.zhongbaopingtai.model.User;
import com.keyancompany.zhongbaopingtai.weight.NoScorllGridView;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;

public class MemberActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    String title = "团队人员";
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.listview)
    ListView listView;
    @Bind(R.id.activity_member)
    LinearLayout activityMember;
    @Bind(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    MemberAdapter memberAdapter;

    String teamCode;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_member;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
        initSwipeLayout(swipeLayout);
    }

    @Override
    protected void setUpViews() {
        teamCode = getIntent().getStringExtra(Constants.TEAM_CODE);
        memberAdapter = new MemberAdapter(null, this);
        listView.setAdapter(memberAdapter);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.post(new Thread(new Runnable() {

            @Override
            public void run() {
                swipeLayout.setRefreshing(true);
                onRefresh();
            }
        }));


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                EventBusEntity entity = new EventBusEntity(EventBusEntity.Type.CHOOSE_ITEM);
                entity.setItem((Serializable) memberAdapter.getItem(i));
                EventBus.getDefault().post(entity);
                finish();
            }
        });

    }

    @Override
    public void onRefresh() {
        FriendsAPI.otherTeamMember(teamCode, this, null);
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        swipeLayout.setRefreshing(false);
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        swipeLayout.setRefreshing(false);
        BaseModel base = validateJson(json);
        if (base != null && base.isSuccess()) {
            JSONObject obj = JSONObject.parseObject(base.data);
            String teamusers = obj.getString("teamusers");
            List<User> users = JSONObject.parseArray(teamusers, User.class);
            memberAdapter.addAllAfter(users);
        }
    }
}
